﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HB.MsgBus;
using HB.MsgBus.MsgBusItem;

namespace FormExample.weakReference
{
    public partial class weakForm : Form
    {
        public weakForm()
        {
            InitializeComponent();
        }
        Random R = new Random();
        private void button3_Click(object sender, EventArgs e)
        {
            GC.Collect();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            MsgBus.Publish<MsgSigns.TransToForm>(t => t(R.Next().ToString()));
        }

        private void weakForm_Load(object sender, EventArgs e)
        {
            MsgBus.OnPublishDoing += MsgBus_OnPublishDoing;
            this.FormClosing += (a, b) =>
            {
                MsgBus.OnPublishDoing -= MsgBus_OnPublishDoing;
            };
        }

        private void MsgBus_OnPublishDoing(PublishInfo arg2)
        {
            this.Invoke(new Action(() =>
            {
                textBox1.AppendText($"{arg2.DelegateName}   ExecBy  {arg2.SubscribeInfo.SubscribeFilePath}---{arg2.SubscribeInfo.SubscribeLineNumber}{Environment.NewLine}");
            }));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            AutoLostSubscribeForm af = new AutoLostSubscribeForm();
            af.Show();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBox1.Clear();
        }
    }
}
